package com.shouxue.pagerv;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity implements PagingScrollHelper.onPageChangeListener{
    //https://www.cnblogs.com/zhujiabin/p/9046461.html
    private ImageView[] dotViews,dotViews2;//创建存放图片集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        RecyclerView mrecRsd = findViewById(R.id.rv);
        //使用通用RecyclerView组件
        PagingScrollHelper scrollHelper = new PagingScrollHelper();//初始化横向管理器
        LinearLayoutManager horizontalPageLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
//        HorizontalPageLayoutManager horizontalPageLayoutManager = new HorizontalPageLayoutManager(1, 3);//这里两个参数是行列，这里实现的是一行三列
        scrollHelper.setUpRecycleView(mrecRsd);//将横向布局管理器和recycler view绑定到一起
        scrollHelper.setOnPageChangeListener(this);//设置滑动监听
        mrecRsd.setLayoutManager(horizontalPageLayoutManager);//设置为横向
        scrollHelper.updateLayoutManger();
        scrollHelper.scrollToPosition(0);//默认滑动到第一页
        mrecRsd.setHorizontalScrollBarEnabled(true);
        RsdTitleAdapter titleAdapter = new RsdTitleAdapter(this,0);//设置适配器
        mrecRsd.setAdapter(titleAdapter);

        List<String> list = new ArrayList<>();
        for (int i = 1; i < 17; i++) {
            list.add("item" + i);
        }
        titleAdapter.setData(list);

        LinearLayout img_layout = findViewById(R.id.dot);
        //生成相应数量的导航小圆点
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置小圆点左右之间的间隔
        params.setMargins(10, 0, 10, 0);
        //得到页面个数
        dotViews = new ImageView[4];//我这里是固定的六页  也可以根据自己需要设置圆点个数
        for (int i = 0; i < 4; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setImageResource(R.drawable.dot);//初始化六个灰色Img
            if (i == 0) {
                //默认启动时，选中第一个小圆点
                imageView.setSelected(true);
            }
            else {
                imageView.setSelected(false);//其他的设置不选择
            }
            //得到每个小圆点的引用，用于滑动页面时，（onPageSelected方法中）更改它们的状态。
            dotViews[i] = imageView;
            dotViews[0].setImageResource(R.drawable.red_dot);//设置第一个页面选择
            //添加到布局里面显示
            img_layout.addView(imageView);//这里的img_layout就是我在布局中写的一个Linear Layout用来存放这些圆点img
        }



        RecyclerView rv2 = findViewById(R.id.rv_half);
        PagingScrollHelper scrollHelper2 = new PagingScrollHelper();//初始化横向管理器
        LinearLayoutManager horizontalPageLayoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        scrollHelper2.setUpRecycleView(rv2);//将横向布局管理器和recycler view绑定到一起
        scrollHelper2.setOnPageChangeListener(new PagingScrollHelper.onPageChangeListener() {
            @Override
            public void onPageChange(int index) {
                for (int i = 0; i < dotViews2.length; i++) {
                    if (index == i) {
                        dotViews2[i].setSelected(true);
                        dotViews2[i].setImageResource(R.drawable.red_dot);
                    } else {
                        dotViews2[i].setSelected(false);
                        dotViews2[i].setImageResource(R.drawable.dot);
                    }
                }
            }
        });//设置滑动监听
        rv2.setLayoutManager(horizontalPageLayoutManager2);//设置为横向
        scrollHelper2.updateLayoutManger();
        scrollHelper2.scrollToPosition(0);//默认滑动到第一页
        rv2.setHorizontalScrollBarEnabled(true);
        RsdTitleAdapter titleAdapter2 = new RsdTitleAdapter(this,1);//设置适配器
        rv2.setAdapter(titleAdapter2);

        List<String> list2= new ArrayList<>();
        for (int i = 1; i < 17; i++) {
            list2.add("item" + i);
        }
        titleAdapter2.setData(list2);
        LinearLayout img_layout2 = findViewById(R.id.dot_half);
        //生成相应数量的导航小圆点
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置小圆点左右之间的间隔
        params2.setMargins(10, 0, 10, 0);
        //得到页面个数
        dotViews2 = new ImageView[4];//我这里是固定的六页  也可以根据自己需要设置圆点个数
        for (int i = 0; i < 4; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setImageResource(R.drawable.dot);//初始化六个灰色Img
            if (i == 0) {
                //默认启动时，选中第一个小圆点
                imageView.setSelected(true);
            }
            else {
                imageView.setSelected(false);//其他的设置不选择
            }
            //得到每个小圆点的引用，用于滑动页面时，（onPageSelected方法中）更改它们的状态。
            dotViews2[i] = imageView;
            dotViews2[0].setImageResource(R.drawable.red_dot);//设置第一个页面选择
            //添加到布局里面显示
            img_layout2.addView(imageView);//这里的img_layout就是我在布局中写的一个Linear Layout用来存放这些圆点img
        }
    }

    @Override
    public void onPageChange(int index) {
        //这里是配合圆点指示器实现的，可以忽略
        for (int i = 0; i < dotViews.length; i++) {
            if (index == i) {
                dotViews[i].setSelected(true);
                dotViews[i].setImageResource(R.drawable.red_dot);
            } else {
                dotViews[i].setSelected(false);
                dotViews[i].setImageResource(R.drawable.dot);
            }
        }
    }

}